<template>
  <div class="app-container">
    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" :disabled="$hasBP('bnt.sysMenu.list')  === false"    @click="add()">添 加</el-button>
    </div>
    <el-table
      empty-text="暂无数据"
      :data="sysMenuList"
      style="width: 100%;margin-bottom: 20px;margin-top: 10px;"
      row-key="id"
      border
      v-loading="listLoading"
      :default-expand-all="false"
      :tree-props="{children: 'children'}">

      <el-table-column prop="name" label="菜单名称" width="160"/>
      <el-table-column label="图标">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="perms" label="权限标识" width="160"/>
      <el-table-column prop="path" label="路由地址" width="120"/>
      <el-table-column prop="component" label="组件路径" width="160"/>
      <el-table-column prop="sortValue" label="排序" width="60"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status === 1" disabled>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160"/>
      <el-table-column label="操作" width="300" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="success" v-if="scope.row.type !== 2" icon="el-icon-plus" size="mini" @click="add(scope.row)"
                     >添加</el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)"
                     :disabled="scope.row.children.length > 0">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹出框-->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
      <el-form ref="dataForm" :model="sysMenu" label-width="150px" size="small" style="padding-right: 40px;">
        <el-form-item label="上级部门" v-if="sysMenu.id === ''">
          <el-input v-model="sysMenu.parentName" disabled/>
        </el-form-item>
        <el-form-item label="菜单类型" prop="type">
          <el-radio-group v-model="sysMenu.type" :disabled="typeDisabled">
            <el-radio :label="0" :disabled="typeDisabled0">目录</el-radio>
            <el-radio :label="1" :disabled="typeDisabled1">菜单</el-radio>
            <el-radio :label="2" :disabled="typeDisabled2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="sysMenu.name"/>
        </el-form-item>
        <el-form-item label="图标" prop="icon" v-if="sysMenu.type !== 2">
          <el-select placeholder="请选择图标" style="width: 75%" v-model="sysMenu.icon" clearable>
            <el-option style="height:100%" v-for="item in iconList" :key="item.class" :label="item.class"
                       :value="item.class">
            <span style="float: left;">
             <i :class="item.class"></i>  <!-- 如果动态显示图标，这里添加判断 -->
            </span>
              <span style="padding-left: 6px;">{{ item.class }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="sysMenu.sortValue" controls-position="right" :min="0"/>
        </el-form-item>
        <el-form-item prop="path">
              <span slot="label">
                <el-tooltip content="访问的路由地址，如：`sysUser`" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                路由地址
              </span>
          <el-input v-model="sysMenu.path" placeholder="请输入路由地址"/>
        </el-form-item>
        <el-form-item prop="component" v-if="sysMenu.type !== 0">
              <span slot="label">
                <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
              <i class="el-icon-question"></i>
                </el-tooltip>
                组件路径
              </span>
          <el-input v-model="sysMenu.component" placeholder="请输入组件路径"/>
        </el-form-item>
        <el-form-item v-if="sysMenu.type === 2">
          <el-input v-model="sysMenu.perms" placeholder="请输入权限标识" maxlength="100"/>
          <span slot="label">
                <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(hasAuthority('bnt.sysRole.list'))"
                            placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                权限字符
              </span>
        </el-form-item>
        <el-form-item label="状态" prop="type">
          <el-radio-group v-model="sysMenu.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" :disabled="saveBtnDisabled" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
const defaultForm = {
  id: '',
  parentId: '',
  name: '',
  type: 0,
  path: '',
  component: '',
  perms: '',
  icon: '',
  sortValue: 1,
  status: 1

}
import menu_icons from '@/icons/sysMenu/Menuicon'
import api from '@/api/system/sysMenu'

export default {
  name: "index",
  data() {
    return {
      listLoading: true,
      sysMenuList: [],
      expandKeys: [], // 需要自动展开的项
      typeDisabled: false,
      typeDisabled0: false,  // 目录
      typeDisabled1: false,  //菜单
      typeDisabled2: false,  // 按钮
      dialogTitle: '',
      dialogVisible: false,
      sysMenu: defaultForm,
      saveBtnDisabled: false,
      iconList: menu_icons.menuIcon
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      api.getMenu().then(res => {
        this.sysMenuList = res.data
        this.listLoading = false
      })
    },
    add(row) {
      this.saveBtnDisabled = false
      this.typeDisabled = false
      this.dialogTitle = '添加菜单'
      this.dialogVisible = true
      this.sysMenu = Object.assign({}, defaultForm)
      this.sysMenu.id = ''
      //  如果是从行点击的新增
      if (row) {
        this.sysMenu.parentId = row.id
        this.sysMenu.parentName = row.name
        if (row.type === 0) {
          this.sysMenu.type = 1
          this.typeDisabled = false
          this.typeDisabled0 = false
          this.typeDisabled1 = false
          this.typeDisabled2 = true
          //  添加 目录下级菜单
        } else if (row.type === 1) {
          //    添加 按钮菜单
          this.sysMenu.type = 2
          //  选 菜单类型的
          this.typeDisabled = true

        }
      } else {
        //   添加 目录
        this.dialogTitle = '添加目录'
        this.sysMenu.type = 0
        this.sysMenu.component = 'Layout'
        this.sysMenu.parentId = 0
        this.typeDisabled = true
      }

    },
    edit(row) {
      this.saveBtnDisabled = false
      this.dialogVisible = '修改菜单'
      this.dialogVisible = true
      this.sysMenu = Object.assign({}, row)
      this.typeDisabled = true

    },
    removeDataById(id) {
      this.$confirm("是否删除该菜单", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.removeById(id).then(res => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })
    },
    saveOrUpdate() {
      if (this.sysMenu.type === 0 && 0 !== this.sysMenu.parentId) {
        this.sysMenu.component = 'ParentView'
      }
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.saveBtnDisabled = true
          if (!this.sysMenu.id) {
            this.save()
          } else {
            this.update()
          }
        }
      })
    },
    save() {
      api.save(this.sysMenu).then(res => {
        this.$message.success('保存成功')
        this.dialogVisible = false
        this.fetchData()
      })
    },
    update() {
      api.update(this.sysMenu).then(res => {
        this.$message.success('修改成功')
        this.dialogVisible = false
        this.fetchData()
      })
    }
  }
}
</script>

<style scoped>
.spacer {
  display: inline-block;
  width: 50px;
}

.el-button--primary {
  background-image: linear-gradient(to right, #00C9FF, #92FE9D);
}

.el-button--success {
  background-image: linear-gradient(to right, #00C9FF, #92FE9D);
}

.el-tree {
  background: linear-gradient(to bottom, #FFFFFF, #CCCCCC);
}

.el-checkbox {
  width: 15px;
  height: 15px;
}
</style>
